<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class AdminLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // 等同于在 config/auth.php 文件中直接定义的效果，更容易控制
        // config(['jwt.user' => '\App\Admin']);    // 用于指定特定model
        // config(['auth.providers.users.model' => \App\Admin::class]); // 就是他们了
        // return $next($request);

        $user = Auth::guard('admin')->user();
        // dump($user);
        // 判断是否管理员登录;则重定向到首页
        if ($user && $user['id'] == 1) {
            return ['access_token' => $request->token, 'token_type' => 'bearer', 'expires_in' => auth('api')->factory()->getTTL() * 60];
        }
        // if (Auth::guard('admin')->check()) {
        //     return redirect('admin/index/index');
        // }
        return $next($request);
    }
}
